//
//  LoginViewController.m
//  LoginAndRegisterDemo
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 Liuwx. All rights reserved.
//

#import "LoginViewController.h"
#import "UserinfoManager.h"
#import "CK_MenuViewController.h"
#import "LWXHomePageController.h"
@interface LoginViewController ()
// 邮箱
@property (weak, nonatomic) IBOutlet UITextField *EmailtextField;
// 密码
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LoginViewController


- (IBAction)didClickBackAction:(id)sender {
    
    
   [self dismissViewControllerAnimated:YES completion:nil];
    
}


// 登录方法

- (IBAction)didClickLoginButtonAction:(id)sender {
    
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/login"];
    NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:url];
    [mRequest setHTTPMethod:@"POST"];
    NSString *bodyStr = [NSString stringWithFormat:@"email=%@&passwd=%@",self.EmailtextField.text,self.passwordTextField.text];
    
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [mRequest setHTTPBody:bodyData];
    
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:mRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (data) {
            NSMutableDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            
          
            
            // 如果登录成功就跳转
            if ([jsonData[@"result"] isEqual:@1]) {
                
//                for (NSString *key in jsonData) {
//                    [[NSUserDefaults standardUserDefaults]setObject:jsonData[key] forKey:key];
//                }
                [[UserinfoManager shareManager] loginWithUserDict:jsonData[@"data"]];
                dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"%@",jsonData);
                    [self dismissViewControllerAnimated:YES completion:nil];
      
                     });
                    
            }else {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:jsonData[@"data"][@"msg"]  preferredStyle:(UIAlertControllerStyleAlert)];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertC animated:YES completion:nil];
            });
       
            }
 
        
        }
        
    }];
    // 启动网络请求
    [dataTask resume];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
